

foreach yr in 11 12 13 14 {

	use "$temp\analysis_claims`yr'.dta", clear

	*drop childrens hospitals and newborns from sample
	drop if serv == "50"
	drop if newborn_flag == 1

	*keep POS/HMO/EPO products
	tab prod, m

	*Drop transfers
	sort z_patid fst_admtdt last_dischdt
	gen flag = 1 if fst_admtdt == last_dischdt[_n-1] & z_patid == z_patid[_n-1]
	replace flag = 1 if fst_admtdt < last_dischdt[_n-1] & z_patid == z_patid[_n-1]
	replace flag = 1 if fst_admtdt + 1 == last_dischdt[_n-1]-1 & z_patid == z_patid[_n-1]
	drop if flag == 1
	drop flag

	*check outliers stays
	assert los>0 & los<=180

	*Drop outlier dollars - secondary payers or payment outliers 
	gen pmnt_ratio = calc_allwd/charge
	drop if pmnt_ratio < .2
	sum pmnt_ratio, det
	drop if pmnt_ratio> r(p99)
	drop pmnt_ratio

	*merge travel times
	merge m:1 pt_zip aha_hnpi using "$temp\hosp dist`yr'.dta", keepus(distance_mi)
	drop if _m == 2
	drop _m

	*Flag outside option choices: gt 150 miles away or fewer than 25 admits in a yr
	gen out_option = distance_mi > 150

	gen obs = 1
	replace obs = 0 if out_option == 1
	bysort aha_hnpi: egen hosp_pts = sum(obs)
	replace out_option = 1 if hosp_pts <= 25
	drop hosp_pts obs

	gen sys_hosp = sysid
	tostring sys_hosp, replace
	replace sys_hosp = aha_hnpi if sys_hosp == "0"

	*Create variables for analysis file
	gen wtd_price = calc_allwd/drg_wt`yr'

	gen female = gdr == 2
	drop gdr

	tab cntrl	
	gen forprofit = inlist(cntrl, 31, 32, 33)
	gen nongovt = inlist(cntrl, 21, 23)

	*create age categories for groupings
	gen age_cat = 1 if age_band_cd == 1
	replace age_cat = 2 if inlist(age_band_cd, 2, 3, 4)
	replace age_cat = 3 if inlist(age_band_cd, 5, 6)
	assert age_cat != .

	save "$temp\Master file`yr'.dta", replace

}

exit
